capture log close
clear all
set maxvar 30000
set more off
pause off
# delimit;

local crime "";
local output "";

/* 
Table B15
IV estimates of SSI removal on joint crime and employment outcomes
*/


/* ********************** */
/* **** PREPARE DATA **** */
/* ********************** */

local mult_age18unfav = 1 ;

foreach type in genr  {;
	foreach endo in age18unfav  {;
		
		use "`crime'/prwora_cXe_bw264_crime_poly1allcov_iv_all.dta", clear ;
		pause ;
				
		duplicates drop variables, force ;
			
		keep if 
				(variables == "n_crime2emp15g_1997_2017" |
				 variables == "n_crime2noemp15g_1997_2017" |
				 variables == "n_crimeno2emp15g_1997_2017" |
				 variables == "n_crimeno2noe15g_1997_2017" |
				 variables == "n_crime2emp15gt_1997_2017" |
				 variables == "n_crime2noemp15gt_1997_2017" |
				 variables == "n_crimeno2emp15gt_1997_2017" |
				 variables == "n_crimeno2noe15gt_1997_2017")
				 ;
					
		replace variables = "cnoXeyes_e" 	if variables == "n_crimeno2emp15g_1997_2017";
		replace variables = "cnoXeno_e"  	if variables == "n_crimeno2noe15g_1997_2017";
		replace variables = "cyesXeyes_e"	if variables == "n_crime2emp15g_1997_2017";
		replace variables = "cyesXeno_e" 	if variables == "n_crime2noemp15g_1997_2017";
				
		replace variables = "cnoXeyes_i" 	if variables == "n_crimeno2emp15gt_1997_2017";
		replace variables = "cnoXeno_i"  	if variables == "n_crimeno2noe15gt_1997_2017";
		replace variables = "cyesXeyes_i"	if variables == "n_crime2emp15gt_1997_2017";
		replace variables = "cyesXeno_i" 	if variables == "n_crime2noemp15gt_1997_2017";
 
		gen b_endo = n_`endo' * `mult_`endo'' ;
		rename sen_`endo' se_endo ;
		keep variables b_endo se_endo mean_rd N ;

	};
};

/* ********************** */
/* **** CREATE TABLE **** */
/* ********************** */

gen id=subinstr(variables, "_i", "", .);
replace id=subinstr(id, "_e", "", .);
egen i = group(id);
drop id;
sort variables;
bys i: gen variable=_n;
order variables b_endo se_endo mean_rd N;

reshape wide variables b_endo se_endo mean_rd N, i(i) j(variable);

gen order=1 if variables1=="cyesXeno_e";
replace order=2 if variables1=="cyesXeyes_e";
replace order=3 if variables1=="cnoXeyes_e";
replace order=4 if variables1=="cnoXeno_e";
sort order;

replace variables1="Crime, no work" if variables1=="cyesXeno_e";
replace variables1="Crime, work" if variables1=="cyesXeyes_e";
replace variables1="No crime, work" if variables1=="cnoXeyes_e";
replace variables1="No crime, no work" if variables1=="cnoXeno_e";

drop i order variables2;
rename (variables1 b_endo1 se_endo1 mean_rd1 b_endo2 se_endo2 mean_rd2) (variables pt_est_iv_ex se_iv_ex mean_ex pt_est_iv_in se_iv_in mean_in);

export excel using "`output'/tabB15.xls", firstrow(variables) ;

